package com.jingdianjichi.subject.infra.basic.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jingdianjichi.subject.infra.basic.entity.Employee;
import com.jingdianjichi.subject.infra.basic.mapper.EmployeeDao;
import com.jingdianjichi.subject.infra.basic.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * 员工表(Employee)表服务实现类
 *
 * @author makejava
 * @since 2024-05-06 16:14:08
 */
@Service("employeeService")
@Slf4j
public class EmployeeServiceImpl implements EmployeeService {

    @Resource
    private EmployeeDao employeeDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Employee queryById(Integer id) {
        return this.employeeDao.queryById(id);
    }




    /**
     * 新增数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee insert(Employee employee) {

        this.employeeDao.insert(employee);
        if (log.isInfoEnabled()){
            log.info("EmployeeController.add.po:{} ", JSON.toJSONString(employee));
        }
        return employee;
    }

    /**
     * 修改数据
     *
     * @param employee 实例对象
     * @return 实例对象
     */
    @Override
    public Employee update(Employee employee) {

        this.employeeDao.update(employee);
        if (log.isInfoEnabled()){
            log.info("EmployeeController.update.po:{} ", JSON.toJSONString(employee));
            log.info("");
        }
        return this.queryById(employee.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        if (log.isInfoEnabled()){
            //log.info("EmployeeController.add.po:{} ", JSON.toJSONString(employee));
            log.info("删除成功");
        }
        return this.employeeDao.deleteById(id) > 0;
    }

    @Override
    public void deleteByIds(List<Integer> ids) {
        if (log.isInfoEnabled()){
            //log.info("EmployeeController.add.po:{} ", JSON.toJSONString(employee));
            log.info("删除成功");
        }
        for (Integer id : ids) {
             this.employeeDao.deleteById(id) ;
        }


    }




    @Override
    public List<Employee> queryAll() {
        // 数据查询成功
        if (log.isInfoEnabled()) {
            log.info("查询全部employee成功");
        }
        return this.employeeDao.queryAll();
    }
//
//        // 返回查询结果
//        PageHelper.startPage(pageNum,pageSize);
//        List<Employee> employees = this.employeeDao.queryAll();
//        System.out.println("employees" +  employees);
//        PageInfo<Employee> pageInfo = new PageInfo<>(employees);
//        long total = pageInfo.getTotal();
//        System.out.println(total);
//        List<Employee> list = pageInfo.getList();
//        System.out.println("list:" + list);
//
//        //通过pageHelper分页
//
//        return list;
//    }




    @Override
    public List<Employee> selectPage(Integer offset, Integer pageSize) {
        System.out.println(offset);
         return this.employeeDao.selectPage(offset, pageSize);
    }

    @Override
    public List<Employee> queryAllElement(Employee employee) {
        return this.employeeDao.queryAllElement(employee);
    }

    @Override
    public String getCurrentNum(String number) {

        String MaxcurrentNum = this.employeeDao.getCurrentNum(number);

        return MaxcurrentNum;

    }
}
